//
//  ViewController.m
//  iOS左右两级联动
//
//  Created by 岚海网络 on 2019/4/4.
//  Copyright © 2019 ljm. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UIBarButtonItem *rightItem;
@property (nonatomic , assign) BOOL isGard;

@property (nonatomic , strong)NSArray *productsArr;
@property (nonatomic , strong)NSArray *sectionArr;
@property (nonatomic , strong) UITableView *tableview;

@property(nonatomic, assign)BOOL isScrollUp;//是否是向上滚动
@property(nonatomic, assign)CGFloat lastOffsetY;//滚动即将结束时scrollView的偏移量

@end

@implementation ViewController

static void createWithUI(ViewController *object)
{
    object.view.backgroundColor = [UIColor whiteColor];
    object.title = @"两级联动";
    
    object.isScrollUp = NO;
    object.lastOffsetY = 0;
    
    object.tableview = [[UITableView alloc] initWithFrame:object.view.frame];
    object.tableview.backgroundColor = [UIColor whiteColor];
    object.tableview.delegate = object;
    object.tableview.dataSource = object;
    object.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    object.tableview.showsVerticalScrollIndicator = NO;
    [object.view addSubview:object.tableview];
}

static void createWithNav(ViewController *object)
{
    object.isGard = YES;
    object.rightItem = [[UIBarButtonItem alloc] initWithTitle:@"网格" style:UIBarButtonItemStylePlain target:object action:@selector(changeWithAction)];
    object.navigationItem.rightBarButtonItem = object.rightItem;
}

static void initWithData(ViewController *obj)
{
    if (!obj.sectionArr) {
        NSArray *numArr = @[@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十"];
        NSMutableArray *tmpArr = [NSMutableArray array];
        for (int i = 0; i < 20; i++) {
            NSString *tmpStr = [NSString stringWithFormat:@"第%@类", numArr[i]];
            [tmpArr addObject:tmpStr];
        }
        obj.sectionArr = tmpArr;
    }
    if (!obj.productsArr) {
        obj.productsArr = @[@"鞋子",@"衣服",@"化妆品",@"饮用水",@"副食品",@"小吃",@"鞋子",@"衣服",@"化妆品",@"饮用水"];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    createWithNav(self);
    createWithUI(self);
    initWithData(self);
}

#pragma mark - uitableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.productsArr.count;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.sectionArr[section];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.productsArr[indexPath.row];
    return cell;
}



#pragma mark - changeWithAction
- (void) changeWithAction
{
    if (self.isGard) {
        self.rightItem.title = @"列表";
        NSLog(@"切换为列表");
    }else
    {
        self.rightItem.title = @"网格";
        NSLog(@"切换为网格");
    }
    self.isGard = !self.isGard;
}

@end
